﻿using System;
using VivendoByteToolkit.Enums;
using VivendoByteToolkit.ViewModels;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace VivendoByteToolkit.Controls
{
    public partial class DatePicker : UserControl
    {
        static DatePickerViewModel vm;

        #region Dependency properties

        #region Date dependency property
        public static DateTime GetDate(DependencyObject obj)
        {
            return (DateTime)vm.DatePicker.GetValue(DateProperty);
        }

        public static void SetDate(DependencyObject obj, DateTime value)
        {
            vm.DatePicker.SetValue(DateProperty, value);
        }

        public static readonly DependencyProperty DateProperty =
            DependencyProperty.RegisterAttached("Date",
            typeof(DateTime),
            typeof(DatePicker),
            new PropertyMetadata(DateTime.Now, new PropertyChangedCallback(Update))); 
        #endregion

        #region DayEnabled dependency property
        public static bool GetDayEnabled(DependencyObject obj)
        {
            return (bool)vm.DatePicker.GetValue(DayEnabledProperty);
        }

        public static void SetDayEnabled(DependencyObject obj, bool value)
        {
            vm.DatePicker.SetValue(DayEnabledProperty, value);
        }

        public static readonly DependencyProperty DayEnabledProperty =
            DependencyProperty.RegisterAttached("DayEnabled",
            typeof(bool),
            typeof(DatePicker),
            new PropertyMetadata(true)); 
        #endregion

        #region MonthEnabled dependency property
        public static bool GetMonthEnabled(DependencyObject obj)
        {
            return (bool)vm.DatePicker.GetValue(MonthEnabledProperty);
        }

        public static void SetMonthEnabled(DependencyObject obj, bool value)
        {
            vm.DatePicker.SetValue(MonthEnabledProperty, value);
        }

        public static readonly DependencyProperty MonthEnabledProperty =
            DependencyProperty.RegisterAttached("MonthEnabled",
            typeof(bool),
            typeof(DatePicker),
            new PropertyMetadata(true));
        #endregion

        #region YearEnabled dependency property
        public static bool GetYearEnabled(DependencyObject obj)
        {
            return (bool)vm.DatePicker.GetValue(YearEnabledProperty);
        }

        public static void SetYearEnabled(DependencyObject obj, bool value)
        {
            vm.DatePicker.SetValue(YearEnabledProperty, value);
        }

        public static readonly DependencyProperty YearEnabledProperty =
            DependencyProperty.RegisterAttached("YearEnabled",
            typeof(bool),
            typeof(DatePicker),
            new PropertyMetadata(true));
        #endregion

        #region SwitchDays dependency property
        public static int GetSwitchDays(DependencyObject obj)
        {
            return (int)vm.DatePicker.GetValue(SwitchDaysProperty);
        }

        public static void SetSwitchDays(DependencyObject obj, int value)
        {
            vm.DatePicker.SetValue(SwitchDaysProperty, value);
        }

        public static readonly DependencyProperty SwitchDaysProperty =
            DependencyProperty.RegisterAttached("SwitchDays",
            typeof(int),
            typeof(DatePicker),
            new PropertyMetadata(2));
        #endregion

        #region SwitchDayMode dependency property
        public static SwitchDayModes GetSwitchDayMode(DependencyObject obj)
        {
            return (SwitchDayModes)vm.DatePicker.GetValue(SwitchDayModeProperty);
        }

        public static void SetSwitchDayMode(DependencyObject obj, SwitchDayModes value)
        {
            vm.DatePicker.SetValue(SwitchDayModeProperty, value);
        }

        public static readonly DependencyProperty SwitchDayModeProperty =
            DependencyProperty.RegisterAttached("SwitchDayMode",
            typeof(SwitchDayModes),
            typeof(DatePicker),
            new PropertyMetadata(SwitchDayModes.Past));
        #endregion

        #region Minimum dependency property
        public static DateTime GetMinimum(DependencyObject obj)
        {
            return (DateTime)vm.DatePicker.GetValue(MinimumProperty);
        }

        public static void SetMinimum(DependencyObject obj, DateTime value)
        {
            vm.DatePicker.SetValue(MinimumProperty, value);
        }

        public static readonly DependencyProperty MinimumProperty =
            DependencyProperty.RegisterAttached("Minimum",
            typeof(DateTime),
            typeof(DatePicker),
            new PropertyMetadata(DateTime.MinValue));
        #endregion

        #region Maximum dependency property
        public static DateTime GetMaximum(DependencyObject obj)
        {
            return (DateTime)vm.DatePicker.GetValue(MaximumProperty);
        }

        public static void SetMaximum(DependencyObject obj, DateTime value)
        {
            vm.DatePicker.SetValue(MaximumProperty, value);
        }

        public static readonly DependencyProperty MaximumProperty =
            DependencyProperty.RegisterAttached("Maximum",
            typeof(DateTime),
            typeof(DatePicker),
            new PropertyMetadata(DateTime.MaxValue));
        #endregion

        #endregion

        #region Public properties
        public DateTime Date
        {
            get
            {
                return DatePicker.GetDate(this);
            }
            set
            {
                DatePicker.SetDate(this, value);
            }
        }

        public bool DayEnabled
        {
            get
            {
                return DatePicker.GetDayEnabled(this);
            }
            set
            {
                DatePicker.SetDayEnabled(this, value);
            }
        }

        public bool MonthEnabled
        {
            get
            {
                return DatePicker.GetMonthEnabled(this);
            }
            set
            {
                DatePicker.SetMonthEnabled(this, value);
            }
        }

        public bool YearEnabled
        {
            get
            {
                return DatePicker.GetYearEnabled(this);
            }
            set
            {
                DatePicker.SetYearEnabled(this, value);
            }
        }

        public int SwitchDays
        {
            get
            {
                return DatePicker.GetSwitchDays(this);
            }
            set
            {
                DatePicker.SetSwitchDays(this, value);
            }
        }

        public SwitchDayModes SwitchDayMode
        {
            get
            {
                return DatePicker.GetSwitchDayMode(this);
            }
            set
            {
                DatePicker.SetSwitchDayMode(this, value);
            }
        }

        public DateTime Minimum
        {
            get
            {
                return DatePicker.GetMinimum(this);
            }
            set
            {
                DatePicker.SetMinimum(this, value);
            }
        }

        public DateTime Maximum
        {
            get
            {
                return DatePicker.GetMaximum(this);
            }
            set
            {
                DatePicker.SetMaximum(this, value);
            }
        } 
        #endregion

        public DatePicker()
        {
            this.InitializeComponent();
            vm = this.Resources["viewmodel"] as DatePickerViewModel;
            vm.DatePicker = this;
        }

        private static void Update(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != e.OldValue)
            {
                vm.Date = (DateTime)e.NewValue;
            }
        }
    }
}